package android.Spider;

import java.util.ArrayList;
import java.util.Date;

import net.idugic.dto.ThreadPostDTO;
import net.idugic.dto.TopicThreadDTO;
import android.os.Bundle;

final public class TopicThread
{
	private static String s_strThreadID = "ThreadID";
	private static String s_strTitle = "Title";
	private static String s_strPosts = "Posts";
	private static String s_strPosterName = "PosterName";
	private static String s_strPostTime = "PostTime";
	private static String s_strPostText = "PostText";
	
	final class Issuer
	{
		String m_strName = new String();
		String m_strTime = new String();
		String m_strText = new String();
		
		void fromDTO(TopicThreadDTO dto)
		{
			m_strName = dto.getOwner_account();
			m_strText = dto.getText();
			m_strTime = DateToString(dto.getThread_time());
		}

		void fromDTO(ThreadPostDTO dto)
		{
			m_strName = dto.getOwner_account();
			m_strText = dto.getPost_text();
			m_strTime = DateToString(dto.getPost_time());
		}
		
		@SuppressWarnings("deprecation")
		private String DateToString(Date date)
		{
			String strText = "";
			strText += date.getDate() + "/" + date.getMonth() + "/" + (date.getYear() + 1900) + " ";
			strText += date.getHours() + ":" + date.getMinutes();
			
			return strText;			
		}
	}

	Issuer CreateIssuer() { return new Issuer(); }
	
	Bundle toBundle()
	{
		Bundle bundle = new Bundle();
		bundle.putInt(s_strThreadID, m_idThread);
		bundle.putString(s_strTitle, m_strThreadName);
		bundle.putInt(s_strPosts, m_lstPosts.size());
		for (int i = 0; i < m_lstPosts.size(); ++i)
		{
			Issuer issuer = m_lstPosts.get(i);
			bundle.putString(s_strPosterName + i, issuer.m_strName);
			bundle.putString(s_strPostTime + i, issuer.m_strTime);
			bundle.putString(s_strPostText + i, issuer.m_strText);
		}
		
		return bundle;
	}
	
	void fromBundle(Bundle bundle)
	{
		m_idThread = bundle.getInt(s_strThreadID);
		m_strThreadName = bundle.getString(s_strTitle);
		final int nSize = bundle.getInt(s_strPosts);
		m_lstPosts = new ArrayList<Issuer>(nSize);
		for (int i = 0; i < nSize; ++i)
		{
			Issuer issuer = CreateIssuer();
			issuer.m_strName = bundle.getString(s_strPosterName + i);
			issuer.m_strTime = bundle.getString(s_strPostTime + i);
			issuer.m_strText = bundle.getString(s_strPostText + i);
			m_lstPosts.add(issuer);
		}
	}
	
	int m_idThread;
	String m_strThreadName = new String();
	ArrayList<Issuer> m_lstPosts = new ArrayList<Issuer>();
}
